//
//  LineDetailViewModel.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/9.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "LineDetailViewModel.h"
#import "RequestLineDetail.h"
#import "UserInfoManager.h"
#import "UserInfoModel.h"
@implementation LineDetailViewModel
-(id)init{
    self.requestLineDetail = [[RequestLineDetail alloc ] init];
    self.upDownStations    = [[NSMutableArray alloc ] init];
    return [super init];
}


-(void)getLineDetailData:(DealDataResultBlock)resultBlock{
    
    WEAKSELF
    [[BaseHttpRequest sharedInstance] sendRequestToPost:LineDetail withData:self.requestLineDetail alertMessage:@"" success:^(NSDictionary *dicData) {
        weakSelf.responseLineDetailModel = [[ResponseLineDetailModel alloc] initWithDictionary:dicData];
        
        // 上下车站点信息。不包含拐角
        [weakSelf.upDownStations removeAllObjects];
        for (UpdownstationModel *upDownModel in weakSelf.responseLineDetailModel.updownstation) {
            if (upDownModel.Type != 4) {
                [weakSelf.upDownStations addObject:upDownModel];
            }
        }
        
        weakSelf.upStationModel  =  [weakSelf findStationModel:weakSelf.responseLineDetailModel.StartStationId];
        weakSelf.downStationModel = [weakSelf findStationModel:weakSelf.responseLineDetailModel.EndStationId];
        
        resultBlock(1,nil,nil);
    } error:^(NSString *errorMsg) {
        resultBlock(0,nil,errorMsg);
    }];
}


-(UpdownstationModel *)findStationModel:(NSInteger)stationId{
    
    for (UpdownstationModel *upDownModel in self.upDownStations) {
        if (upDownModel.id == stationId) {
            return upDownModel;
        }
    }
    return nil;
}

// 直通车线路详情
-(void)getThroughLineDetailData:(DealDataResultBlock)resultBlock{
    WEAKSELF
    [[BaseHttpRequest sharedInstance] sendRequestToPost:ThroughBusLineDetail withData:self.requestLineDetail alertMessage:@"" success:^(NSDictionary *dicData) {
        weakSelf.responseLineDetailModel = [[ResponseLineDetailModel alloc] initWithDictionary:dicData];
        
        // 上下车站点信息。不包含拐角
        [weakSelf.upDownStations removeAllObjects];
        for (UpdownstationModel *upDownModel in weakSelf.responseLineDetailModel.updownstation) {
            if (upDownModel.Type != 4) {
                [weakSelf.upDownStations addObject:upDownModel];
            }
        }
        
        weakSelf.upStationModel  =  [weakSelf findStationModel:weakSelf.responseLineDetailModel.StartStationId];
        weakSelf.downStationModel = [weakSelf findStationModel:weakSelf.responseLineDetailModel.EndStationId];
        
        resultBlock(1,nil,nil);
    } error:^(NSString *errorMsg) {
        resultBlock(0,nil,errorMsg);
    }];
}

-(void)getWaiteLineDetailData:(DealDataResultBlock)resultBlock{
    WEAKSELF
    [[BaseHttpRequest sharedInstance] sendRequestToPost:WaiteLineDetail withData:self.requestLineDetail alertMessage:@"" success:^(NSDictionary *dicData) {
        weakSelf.responseLineDetailModel = [[ResponseLineDetailModel alloc] initWithDictionary:dicData];
        
        // 上下车站点信息。不包含拐角
        [weakSelf.upDownStations removeAllObjects];
        
        
        UpdownstationModel *startUpDownModel = [[UpdownstationModel alloc ] init];
        startUpDownModel.Lat = weakSelf.responseLineDetailModel.StartLat;
        startUpDownModel.Lng = weakSelf.responseLineDetailModel.StartLng;
        startUpDownModel.StationName = weakSelf.responseLineDetailModel.StartStation;
        startUpDownModel.Type = 0;
        [weakSelf.upDownStations addObject:startUpDownModel];
        
        UpdownstationModel *endUpDownModel = [[UpdownstationModel alloc ] init];
        endUpDownModel.Lat = weakSelf.responseLineDetailModel.EndLat;
        endUpDownModel.Lng = weakSelf.responseLineDetailModel.EndLng;
        endUpDownModel.StationName = weakSelf.responseLineDetailModel.EndStation;
        endUpDownModel.Type = 3;
        [weakSelf.upDownStations addObject:endUpDownModel];
        
        
        weakSelf.responseLineDetailModel.updownstation = weakSelf.upDownStations;
        
        resultBlock(1,nil,nil);
    } error:^(NSString *errorMsg) {
        resultBlock(0,nil,errorMsg);
    }];
}

-(void)loadLineDetailAction:(NSString *)action resultBlock:(DealDataResultBlock)resultBlock{
    
}

-(void)lineSignUp:(DealDataResultBlock)resultBlock{
    
    NSMutableDictionary *postDic = [[NSMutableDictionary alloc ] init];
    [postDic setObj:USERINFOManager.getUserInfo.MemberId forKey:@"MemberId"];
    [postDic setObj:self.requestLineDetail.LineId forKey:@"LineId"];
    
  
    [[BaseHttpRequest sharedInstance] sendRequestToPost:LineSignUp withData:postDic alertMessage:@"" success:^(NSDictionary *dicData) {
        resultBlock(1,nil,nil);
    } error:^(NSString *errorMsg) {
        resultBlock(0,nil,errorMsg);
    }];
}

@end
